
function match_cancel(id, tournament_id){
	if(confirm("Are you sure to cancel the match?")){
		jQuery.post(BASE_URI+'admin/dataman/match/match_cancel',{match_id:id, tournament_id:tournament_id},function(res){
			if(res== 'ok'){
				reload();
			}else{
				alert(res);
			}
		});
	}
}

function event_category_cancel(event_category_id){
	jQuery.post(BASE_URI+'admin/dataman/match/event_category_cancel',{event_category_id:event_category_id},function(res){
		var p = res.split('#');
		if(p[0]== 'ok'){
			var match_id = p[1];
			$mode = jQuery('#dialog_mode').val();
			if($mode == 'open'){
				view_event(match_id);
			}else{
				view_event_hidden_category(match_id);
			}
		}else{
			alert(res);
		}
	});
}

function event_category_finish(category_id){
	jQuery.post(BASE_URI+'admin/dataman/match/event_category_finish',{event_category_id:category_id},function(res){
		var p = res.split('#');
		if(p[0]== 'ok'){
			var match_id = p[1];
			$mode = jQuery('#dialog_mode').val();
			if($mode == 'open'){
				view_event(match_id);
			}else{
				view_event_hidden_category(match_id);
			}
		}else{
			alert(res);
		}
	});
}

function event_cancel(event_id){
	jQuery.post(BASE_URI+'admin/dataman/match/event_cancel',{event_id:event_id},function(res){
		var p = res.split('#');
		if(p[0]== 'ok'){
			var match_id = p[1];
			$mode = jQuery('#dialog_mode').val();
			if($mode == 'open'){
				view_event(match_id);
			}else{
				view_event_hidden_category(match_id);
			}
		}else{
			alert(res);
		}
	});
}

function event_finish(event_id){
	jQuery.post(BASE_URI+'admin/dataman/match/event_finish',{event_id:event_id},function(res){
		var p = res.split('#');
		if(p[0]== 'ok'){
			var match_id = p[1];
			$mode = jQuery('#dialog_mode').val();
			if($mode == 'open'){
				view_event(match_id);
			}else{
				view_event_hidden_category(match_id);
			}
		}else{
			alert(res);
		}
	});
}

function match_finish(id, tournament_id){
	jQuery.post(BASE_URI+'admin/dataman/match/match_finish',{id:id,tournament_id:tournament_id},function(res){
		if(res== 'ok'){
			reload();
		}else{
			alert(res);
		}
	});
}

function set_event_result(event_id, result){
	jQuery.post(BASE_URI+'admin/dataman/match/set_event_result',{id:event_id,result:result},function(res){
		//view_event(res);
	});
}

function who_bet_event(event_id){
	jQuery.post(BASE_URI+'admin/dataman/match/who_bet_event',{id:event_id},function(res){
		jQuery('#dialog2').html(res);
		jQuery('#dialog2').dialog(
			{
				 width: 700,
				 resizable: false,
				 height:400,
				 title: 'User bet event' 
			}
		);
	});
}

function change_bet_status_match(id){
	jQuery.post(BASE_URI+'admin/dataman/match/change_bet_status_match',{id:id},function(res){
		if(res== 'ok'){
			reload();
		}else{
			alert(res);
		}
	});
}

function event_category_open_hide(category_id,match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/event_category_open_hide',{category_id:category_id,match_id:match_id},function(res){
		if(res== 'ok'){
			$mode = jQuery('#dialog_mode').val();
			if($mode == 'open'){
				view_event(match_id);
			}else{
				view_event_hidden_category(match_id);
			}
		}else{
			alert(res);
		}
	});
}

function event_change_bet_status(event_id,match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/event_change_bet_status',{id:event_id},function(res){
		if(res== 'ok'){
			$mode = jQuery('#dialog_mode').val();
			if($mode == 'open'){
				view_event(match_id);
			}else{
				view_event_hidden_category(match_id);
			}	
		}else{
			alert(res);
		}
	});
}

function add_new_match(tournament_id){
	jQuery.post(BASE_URI+'admin/dataman/match/add_new_match',{tournament_id:tournament_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:400,
				 title: 'Add match' 
			}
		);
	});
}

function match_edit(id, tournament_id){
	jQuery.post(BASE_URI+'admin/dataman/match/edit_match',{id:id, tournament_id:tournament_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:400,
				 title: 'Edit match' 
			}
		);
	});
}

function function_of_event(task, match_id,$this){
	switch(task){
		case 'add_category_event':
			add_category_event(match_id);
			break;
		case 'view_category_event':
			view_category_event(match_id);
			break;
		case 'add_event':
			add_event(match_id);
			break;
		case 'view_event':
			view_event(match_id);
			break;
	}
	
	jQuery('.eventslb').prop('selectedIndex',0);
	
}

function add_category_event(match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/add_category_event',{match_id:match_id},function(res){
		jQuery('#dialog3').html(res);
		jQuery('#dialog3').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:200,
				 title: 'Add Category Event' 
			}
		);
	});
}

function view_category_event(match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/view_category_event',{match_id:match_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 650,
				 resizable: true,
				 height:400,
				 title: 'View Category Event' 
			}
		);
	});
}

function event_category_edit(event_category_id){
	jQuery.post(BASE_URI+'admin/dataman/match/event_category_edit',{id:event_category_id},function(res){
		jQuery('#dialog2').html(res);
		jQuery('#dialog2').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:200,
				 title: 'Edit Category Event' 
			}
		);
	});
}

function add_event(match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/add_event',{match_id:match_id},function(res){
		jQuery('#dialog3').html(res);
		jQuery('#dialog3').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:400,
				 title: 'Add Event' 
			}
		);
	});
}

function openOrderingDialog(match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/ordering_category',{match_id:match_id},function(res){
		jQuery('#dialog3').html(res);
		jQuery('#dialog3').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:400,
				 title: 'Ordering Category' ,
				 zIndex: 1015
			}
		);
		jQuery('#sortable').sortable();
	});

}

function view_event(match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/view_event',{match_id:match_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 1020,
				 resizable: false,
				 height:550,
				 title: 'View Event: Live/UpComing' ,
				 zIndex: 1005
			}
		);
	});
}

function view_event_closed(match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/view_event_closed',{match_id:match_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 1020,
				 resizable: false,
				 height:550,
				 title: 'View Event: Closed' 
			}
		);
	});
}

function view_event_cancelled(match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/view_event_cancelled',{match_id:match_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 1020,
				 resizable: false,
				 height:550,
				 title: 'View Event: Cancelled' 
			}
		);
	});
}

function view_event_hidden_category(match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/view_event_hidden_category',{match_id:match_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 1020,
				 resizable: false,
				 height:550,
				 title: 'View Event: Hidden Category' 
			}
		);
	});
}


function event_edit(event_id, match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/edit_event',{match_id:match_id,id:event_id},function(res){
		jQuery('#dialog2').html(res);
		jQuery('#dialog2').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:400,
				 title: 'Edit Event' 
			}
		);
	});
}

function add_events_to_category(category_id, match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/add_events_to_category',{match_id:match_id,category_id:category_id},function(res){
		jQuery('#dialog2').html(res);
		jQuery('#dialog2').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:600,
				 title: 'Add Events To This Category' 
			}
		);
	});
}

function add_more_event(category_id,match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/add_more_event',{category_id:category_id,match_id:match_id},function(res){
		jQuery('#addMore').append(res);
	});
}

function remove_event(uid){
	jQuery('#wrap_'+uid).remove();
}

function edit_all_events_of_category(category_id,match_id){
	jQuery.post(BASE_URI+'admin/dataman/match/edit_all_events_of_category',{match_id:match_id,category_id:category_id},function(res){
		jQuery('#dialog2').html(res);
		jQuery('#dialog2').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:600,
				 title: 'Edit Events Of This Category' 
			}
		);
	});
}

jQuery(function() {
	
	jQuery("#start_time_btn").live('click',function(){
		jQuery('.DynarchCalendar-topCont').css({top:100,width:60});
		Calendar.setup({
			inputField : "start_time",
			trigger    : "start_time_btn",
			onSelect   : function() { this.hide() },
			showTime   : 24,
			dateFormat : "%Y-%m-%d %I:%M"
		});
	});
	jQuery("#end_time_btn").live('click',function(){
		jQuery('.DynarchCalendar-topCont').css({top:100,width:60});
		Calendar.setup({
			inputField : "end_time",
			trigger    : "end_time_btn",
			onSelect   : function() { this.hide() },
			showTime   : 24,
			dateFormat : "%Y-%m-%d %I:%M"
		});
	});
	
});

jQuery(document).ready(function() {
	var optionsMatch = { 
		beforeSubmit:beforeSubmitMatch,   
		success:afterSubmitMatch
	};	
	var optionsEventCategory = {
		beforeSubmit:beforeSubmitEventCategory,
		success:afterSubmitEventCategory
	};
	var optionsEvent = { 
		beforeSubmit:beforeSubmitEvent,   
		success:afterSubmitEvent
	};	
	var optionsEvent2 = { 
		beforeSubmit:function(formData, jqForm, options){return true;},   
		success:function(responseText, statusText, xhr, $form){
			if(isNaturalNumber(responseText)){
				//jQuery('#dialog').dialog('close');
				jQuery('#dialog2').dialog('close');
				jQuery('#dialog3').dialog('close');
			 
				$mode = jQuery('#dialog_mode').val();
				if($mode == 'open'){
					view_event(responseText);
				}else{
					view_event_hidden_category(responseText);
				}
			}else{
				alert(responseText);
			}
		}
	};	
		
	jQuery("#match_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsMatch);
		return false;
	});	
	jQuery("#event_category_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsEventCategory);
		return false;
	});	
	jQuery("#event_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsEvent);
		return false;
	});	
	jQuery("#add_events_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsEvent2);
		return false;
	});	
	jQuery("#edit_events_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsEvent2);
		return false;
	});	
	
	jQuery('#button_save_ordering').live('click',function(){
		jQuery('#loader_image_ordering').show();
		$match_id = jQuery('#match_id_ordering_form').val();
		$idArray = [];
		jQuery('#sortable .ui-state-default').each(function(i,o){
			$idArray.push(jQuery(this).attr('id'));
		});
		$ids = $idArray ? $idArray.join(','):'';
		jQuery.post(BASE_URI+'admin/dataman/match/update_ordering_category',{ids:$ids,match_id:$match_id},function(res){
			if(res == 'ok'){
				 
				$mode = jQuery('#dialog_mode').val();
				if($mode == 'open'){
					view_event($match_id);
				}else{
					view_event_hidden_category($match_id);
				}
				jQuery('#dialog3').dialog( "close" );
			}else{
				alert(res);
			}
		});
	});
});

function beforeSubmitEvent(formData, jqForm, options){
	jQuery('#loader').show();
	
	json = {};
	for( e in formData){
		json[formData[e].name] = formData[e].value;
	}
	
	var arrayRequire = [ 'name', 'odds' ]; 
	var err = 0;
	for(var i=0 ;i< arrayRequire.length; i++ ){
		if( isEmpty(json[arrayRequire[i]]) ){
			jQuery('#'+arrayRequire[i]+'_error').show();
			err++;
		}else{
			jQuery('#'+arrayRequire[i]+'_error').hide();
		}
	}
	
	if(err > 0){	
		jQuery('#loader').hide();
		alert("Error. Any fields are missing.");
		return false;
	}else{
		return true;
	}
}

function afterSubmitEvent(responseText, statusText, xhr, $form){
	jQuery('#loader').hide();	
	 
	if(isNaturalNumber(responseText)){
		//jQuery('#dialog').dialog('close');
		jQuery('#dialog2').dialog('close');
		jQuery('#dialog3').dialog('close');
		
		$mode = jQuery('#dialog_mode').val();
		if($mode == 'open'){
			view_event(responseText);
		}else{
			view_event_hidden_category(responseText);
		}
		
	}else{
		alert(responseText);
	}
}

function beforeSubmitEventCategory(formData, jqForm, options){
	jQuery('#loader').show();
	
	json = {};
	for( e in formData){
		json[formData[e].name] = formData[e].value;
	}
	
	var arrayRequire = [ 'category' ]; 
	var err = 0;
	for(var i=0 ;i< arrayRequire.length; i++ ){
		if( isEmpty(json[arrayRequire[i]]) ){
			jQuery('#'+arrayRequire[i]+'_error').show();
			err++;
		}else{
			jQuery('#'+arrayRequire[i]+'_error').hide();
		}
	}
	
	if(err > 0){	
		jQuery('#loader').hide();
		alert("Error. Any fields are missing.");
		return false;
	}else{
		return true;
	}
} 
 
function afterSubmitEventCategory(responseText, statusText, xhr, $form){
	jQuery('#loader').hide();	
	 
	if(isNaturalNumber(responseText)){
		//jQuery('#dialog').dialog('close');
		jQuery('#dialog2').dialog('close');
		jQuery('#dialog3').dialog('close');
		
		$mode = jQuery('#dialog_mode').val();
		if($mode == 'open'){
			view_event(responseText);
		}else{
			view_event_hidden_category(responseText);
		}
	}else{
		alert(responseText);
	}
} 

function afterSubmitMatch(responseText, statusText, xhr, $form) {
	jQuery('#loader').hide();	
	 
	if(responseText == 'ok'){
		jQuery('#dialog').dialog('close');
		reload();
	}else{
		alert(responseText);
	}
}

function beforeSubmitMatch(formData, jqForm, options){
	jQuery('#loader').show();
	
	json = {};
	for( e in formData){
		json[formData[e].name] = formData[e].value;
	}
	
	var arrayRequire = [ 'start_time','end_time']; 
	var err = 0;
	for(var i=0 ;i< arrayRequire.length; i++ ){
		if( isEmpty(json[arrayRequire[i]]) ){
			jQuery('#'+arrayRequire[i]+'_error').show();
			err++;
		}else{
			jQuery('#'+arrayRequire[i]+'_error').hide();
		}
	}
	
	if(err > 0){	
		jQuery('#loader').hide();
		alert("Error. Any fields are missing.");
		return false;
	}else{
		return true;
	}
}